﻿/// <reference path="../Libriries/jquery-2.0.3.js" />
/// <reference path="../Libriries/q.js" />
/// <reference path="class.js" />
/// <reference path="lecture-controller.js" />

var LecturesMaker = (function () {
    var LECTURES_PATH = "../Resources/XmlRes/BG/Programming/";

    var lectures = [];

    var makeLectures = function (selector, course, path) {
        lectures = [];

        if (course == "C# I") {
            makeCsharpLectures(selector, path);
        }
        //TODO: Add other courses
    }

    var makeCsharpLectures = function(selector, path) {
        var holder = $(selector);
        holder.html(null);
        var fragment = document.createDocumentFragment();
        var counter = 1;

        $.ajax({
            type: 'GET',
            url: LECTURES_PATH + path,
            dataType: 'xml',
            success: function (xml) {
                $(xml).find("lectures lecture").each(function () {
                    var header = $(this).attr("header");
                    var name = $(this).attr("name");
                    var picPath = $(this).attr("pic");
                    var video = $(this).attr("video");
                    var presentation = $(this).attr("presentation");
                    var materials = $(this).attr("materials");
                    var content = $(this).attr("content");

                    var position = "left";
                    if (counter % 2 == 0) {
                        position = "right";
                    }
                    counter++;

                    var lecture = new Lecture(header, name, picPath, position, video, presentation, materials, content);

                    $(this).find("p").each(function () {
                        var text = $(this).text();
                        lecture.addParagraph(text);
                    });

                    holder.append(lecture.frame);
                });

            },
            error: function (error) {
                $(holder).html(null);
                $(holder).append("<span style='color: white;'>The loading of lectures encountered an error. Please contact admin if possible</span>");
                console.log(error);
            }
        });
    }

    var Lecture = Class.create({
        init: function (header, name, picPath, position, video, presentation, materials, content) {
            var self = this;

            this.header = header;
            this.name = name;
            this.picPath = picPath;
            this.content = content;
            this.position = position;
            this.oppositePosition;

            if (position == "left") {
                this.oppositePosition = "right";
            }
            else {
                this.oppositePosition = "left";
            }

            this.frame = $("<article class='lecture'></article>");

            this.lectureMain = $("<div class='lecture-main " + position + "'></div>");
            this.lectureMain.css({
                'background-image': 'url(../Resources/Images/Lectures/' + this.picPath + ')'
            });
            this.lectureMain.click(function () {
                LectureController.loadLecture(self.name, self.content);
            });
            var h1 = $("<h1></h1>");
            h1.text(header);
            var p = $("<p></p>");
            p.text(name);

            this.lectureMain.append(h1);
            this.lectureMain.append(p);

            this.lectureSummary = $("<div class='lecture-summary " + this.oppositePosition + "'></div>");

            this.buttonsHolder = $("<div class='buttons-holder " + this.oppositePosition + "'></div>");
            this.btnVideo = $("<a href='" + video + "' class='btn-video button' title='Video'></a>");
            this.btnPresentation = $("<a href='" + presentation + "' class='btn-presentation button' title='Presentation'></a>");
            this.btnMaterials = $("<a href='" + materials + "' class='btn-materials button' title='Files'></a>");

            this.buttonsHolder.append(this.btnVideo);
            this.buttonsHolder.append(this.btnPresentation);
            this.buttonsHolder.append(this.btnMaterials);

            this.frame.append(this.lectureMain);
            this.frame.append(this.lectureSummary);
            this.frame.append(this.buttonsHolder);
        },
        addParagraph: function (text) {
            var paragraph = $("<p class='text-" + this.oppositePosition + "'></p>");
            paragraph.text(text);

            this.lectureSummary.append(paragraph);
        }
    });

    var changeLecturesLanguage = function (language) {
        LECTURES_PATH = "../Resources/XmlRes/" + language + "/Programming/";
    }

    return {
        makeLectures: makeLectures,
        makeCsharpLectures: makeCsharpLectures,
        changeLecturesLanguage: changeLecturesLanguage
    }
}());